<?php
namespace app\admin\model;
use think\Exception;
use think\facade\Db;

class MyBeiFen
{
    protected $config = [
        'path'=>'../application/admin/config',
        'name'=>'default',
        'db'=>'',
        'table'=>'',
        'number'=>0
    ];
    public function __construct($config=[]){
        foreach ($config as $key=>$value){
            $this->config[$key] = $value;
        }
        $this->config['db'] = Db::connect();
    }
    public function backups($tables=[]){
        foreach ($tables as $value){
            $this->backup($value);
        }
        return $this->config['path'].'/'.$this->config['name'].'-1.sql';
    }
    public function backup($table){
        $array = Db::query('show create table '.$table);
        $str = 'DROP TABLE IF EXISTS `'.$table.'`;'.PHP_EOL;
        $str .= $array[0]['Create Table'].';'.PHP_EOL;
        if(!is_dir($this->config['path'])){
            mkdir($this->config['path']);
        }
        $this->config['table'] = $table;
        $datas = $this->config;
        file_put_contents(rtrim($this->config['path'],'/').'/'.$this->config['name'].'-1.sql',$str,FILE_APPEND);
        $data = Db::table($table)->chunk(1000,function ($values)use ($datas){
            $name = $datas['table'].'-'.uniqid().'.php';
            $str = "<?php".PHP_EOL;
            $str .= 'return ['.PHP_EOL;
            file_put_contents($datas['path'].'/'.$name,$str);
            foreach ($values as $value){
                $str = '['.PHP_EOL;
                foreach ($value as $key=>$va){
                    $str .= "'$key'=>'$va',".PHP_EOL;
                }
                $str .= '],'.PHP_EOL;
                file_put_contents($datas['path'].'/'.$name,$str,FILE_APPEND);
            }
            $str = '];';
            file_put_contents($datas['path'].'/'.$name,$str,FILE_APPEND);
        });
        return true;
    }
    public function getlist($project){
        $array = Db::query('show tables');
        $newname = [];
        $zhenze = '/'.config('database.prefix').$project.'_/';
        foreach ($array as $value){
            if(preg_match($zhenze,$value['Tables_in_'.config('database.database')])){
                $newname[] = $value['Tables_in_'.config('database.database')];
            }
        }
        return $newname;
    }
    public function addinserts($tables,$project){
        $array = [];
        foreach ($tables as $value){
            $array[] =  $this->add($value,$project);
        }
        return true;
    }
    public function add($table,$project){
        $list = $this->getdir("../application/".$project.'/Data/');
//        return $list;
        $newlist = [];
        foreach ($list as $value){
//            $newlist[] = $table;
            if(strstr($value,$table)){
//                $newlist[] = $value;
                $path = "../application/".$project.'/Data/'.$value;
                $array = include $path;
                try{
                    Db::table($table)->insertAll($array);
                }catch (Exception $e){
                    return $table.'出错';
                }
            }
        }

        return true;
    }
    public function getdir($project){
        if(!is_dir($project)){
            return [];
        }
        $dir =$project;
        $dir = scandir($dir);
        $newdir = [];
        foreach ($dir as $value){
            if($value!=='.'&&$value!=='..'){
                $newdir[] = $value;
            }
        }
        return $newdir;
    }

}